package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.wf.smartcomm.server.bean.UnitMachine;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.UnLockService;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-21 下午2:19:07 
 * 
 */
@Service
@Transactional
public class UnLockServiceImpl implements UnLockService {

	@Resource
	private SessionFactory factory;

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByUm(String mid, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from UnLock where mid =:mid");
		query.setParameter("mid", mid);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from UnLock where mid =:mid order by createdAt desc");
		query.setParameter("mid", mid);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void queryByKey(String mid, String key, Page page) {
		String hql = "select count(*) from UnLock where mid =:mid and uid in (select objectId from User where name like :key)";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("mid", mid);
		query.setParameter("key", "%"+key+"%");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		hql = "from UnLock where mid =:mid and uid in (select objectId from User where name like :key order by createdAt desc)";
		query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("mid", mid);
		query.setParameter("key", "%"+key+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

}
